package org.loofer.ext.utils;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.text.TextUtils;
import java.util.List;

public final class AppUtils {
    private static Bundle sOwnAppMetaInfo;

    private AppUtils() {
    }

    public static boolean isForeground(Context context) {
        if (isScreenLocked(context)) {
            return false;
        }
        List<RunningTaskInfo> a = ((ActivityManager) context.getSystemService("activity")).getRunningTasks(1);
        boolean z = context.getPackageName().equals(((RunningTaskInfo) a.get(0)).baseActivity.getPackageName()) && context.getPackageName().equals(((RunningTaskInfo) a.get(0)).topActivity.getPackageName());
        return z;
    }

    private static boolean isScreenLocked(Context context) {
        return ((KeyguardManager) context.getSystemService("keyguard")).inKeyguardRestrictedInputMode();
    }

    public static int getVersionCode(Context context) {
        int i = 0;
        if (context != null) {
            try {
                i = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
            } catch (Throwable th) {
            }
        }
        return i;
    }

    public static ApplicationInfo getAppInfoWithFlags(Context ctx, int flags) {
        ApplicationInfo applicationInfo = null;
        if (ctx != null) {
            try {
                applicationInfo = ctx.getPackageManager().getApplicationInfo(ctx.getPackageName(), 128);
            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        return applicationInfo;
    }

    public static ApplicationInfo getSimpleAppInfo(Context ctx) {
        return ctx == null ? null : ctx.getApplicationInfo();
    }

    public static boolean isPackageInstalled(Context context, String packageName) {
        if (TextUtils.isEmpty(packageName)) {
            return false;
        }
        try {
            if (context.getPackageManager().getPackageInfo(packageName, 0) != null) {
                return true;
            }
            return false;
        } catch (NameNotFoundException e) {
            return false;
        }
    }

    public static Bundle getApplicationMetaInfo(Context context) {
        if (sOwnAppMetaInfo == null) {
            synchronized (AppUtils.class) {
                if (sOwnAppMetaInfo == null) {
                    sOwnAppMetaInfo = getApplicationMetaInfo(context, context.getPackageName());
                }
            }
        }
        return sOwnAppMetaInfo != null ? new Bundle(sOwnAppMetaInfo) : null;
    }

    public static Bundle getApplicationMetaInfo(Context context, String packageName) {
        ApplicationInfo appInfo;
        Bundle metaInfo = null;
        if (packageName.equals(context.getPackageName())) {
            appInfo = context.getApplicationInfo();
        } else {
            appInfo = null;
        }
        if (appInfo != null) {
            metaInfo = appInfo.metaData;
        }
        if (metaInfo != null) {
            return metaInfo;
        }
        try {
            return context.getPackageManager().getApplicationInfo(packageName, 128).metaData;
        } catch (Throwable th) {
            return metaInfo;
        }
    }
}
